package guda.house.web.action.front;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.VillageBiz;
import guda.house.biz.WxAccessBiz;
import guda.house.biz.form.front.BindSuiteForm;
import guda.house.biz.query.VillageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by well on 16/5/27.
 */
@Controller
@RequestMapping(value = "front")
public class BindSuiteAction {

    @Autowired
    private VillageBiz villageBiz;
    @Autowired
    private WxAccessBiz wxAccessBiz;

    @RequestMapping(value = "bindSuite.htm", method = RequestMethod.GET)
    public String bindSuite(HttpServletRequest request, ModelMap modelMap, BindSuiteForm bindSuiteForm,
                            BindingResult result) {
        VillageQuery villageQuery = new VillageQuery();
        villageQuery.setPageSize(1000);
        BizResult list = villageBiz.list(villageQuery);
        modelMap.putAll(list.data);

        return "front/bindSuite.vm";
    }


    @RequestMapping(value = "bindSuite/doBind.htm")
    public String doBind(HttpServletRequest request, ModelMap modelMap,@Valid BindSuiteForm bindSuiteForm,
                          BindingResult result) {
        if (result.hasErrors()) {
            return "front/bindSuite.vm";
        }
        BizResult bizResult = wxAccessBiz.bindSuite(bindSuiteForm.getSuiteId());
        if(!bizResult.success){
            return "redirect:/front/error.htm?code=" + bizResult.code;
        }
        return "front/bindSuiteSuccess.vm";
    }

    @RequestMapping(value = "bindSuite/doUnBind.htm")
    public String doUnBind(HttpServletRequest request, ModelMap modelMap) {
        long suiteId = RequestUtil.getLong(request,"suiteId");
        BizResult bizResult = wxAccessBiz.unBindSuite(suiteId);
        if(!bizResult.success){
            return "redirect:/front/error.htm?code=" + bizResult.code;
        }
        return "front/unBindSuiteSuccess.vm";
    }
}
